﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using XT.BusinessService;
using XT.Model;
using XT.Web.External;

namespace XT.Web.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            var roomservice = MvcApplication.GetInstance<IRoomService>();
            var listroombydate = roomservice.getThreeRoomByDate();
            var listroombyviews = roomservice.getThreeRoomByViews();
            return View(Tuple.Create(listroombydate, listroombyviews));
            
        }

        public ActionResult RegisterFunc(FormCollection col)
        {
           
            string name = col["name"];
            string username = col["username"];
            string password = col["password"];
            string mail = col["mail"];                      

            Account acc = new Account();

            acc.Account_Username = username;
            acc.Account_Password = password;
            acc.Account_Name = name;
            acc.Account_Email = mail;

            var accountService = MvcApplication.GetInstance<IAccountService>();
            if (accountService.checkExistEmail(mail) == 1 && accountService.checkExistUsername(username) == 1)
            {
                
               if (accountService.AddAccount(acc) == 1)
                {
                    return Json(new { success=true});
                }

                return Json(new { success = false });
            }
            if (accountService.checkExistEmail(mail) != 1)
            {
               
                return Json(new { check = 1 });
            }
            if (accountService.checkExistUsername(username) != 1)
            {
                return Json(new { check = 2 });
            }

            return View();     
           
        }
        public ActionResult Register()
        {
            return View("Register");
        }

        public ActionResult LoginFunc(FormCollection col)
        {
            var accountService = MvcApplication.GetInstance<IAccountService>();
            string mail = col["email"];
            string password = col["pass"];
            string id = accountService.checkLogin(mail, password).Id.ToString();
            var remember = (bool)ValueProvider.GetValue("remember").ConvertTo(typeof(bool));
            //JavaScriptSerializer se = new JavaScriptSerializer();
            //string json = Json(accountService.getAccountById(Int32.Parse(id))).ToString();
           // var model = se.DeserializeObject(json);
            if (accountService.checkLogin(mail, password) != null)
            {
                HttpCookie UserCookie = new HttpCookie("Id", id);
                if (remember == true)
                {
                    UserCookie.Expires = DateTime.Today.AddDays(30);
                }
                else
                {
                    UserCookie.Expires = DateTime.Today.AddDays(1);
                }
                Response.Cookies.Add(UserCookie);
             
                return Json(new { success=true,id=id});
            }
            return Json(new { success = false });
        }

        public ActionResult Login()
        {
            return View("Login");
        }
        public ActionResult Hot()
        {
            var roomservice = MvcApplication.GetInstance<IRoomService>();
            var lstRoomHot = roomservice.showMoreHotRoom(1, 6);
            return View(lstRoomHot);
        }
        public ActionResult ShowMoreHot(int pageIndex)
        {
            var roomservice = MvcApplication.GetInstance<IRoomService>();
            var lstRoomHot = roomservice.showMoreHotRoom(pageIndex + 1, 6);
            return Json(new { content = Helper.RenderViewToString(this.ControllerContext, "../Global/Rooms", lstRoomHot) });
        }

        public ActionResult New()
        {
            var roomservice = MvcApplication.GetInstance<IRoomService>();
            var lstRoomNew = roomservice.showMoreNewRoom(1, 6);
            return View(lstRoomNew);
        }
        public ActionResult ShowMoreNew(int pageIndex)
        {
            var roomservice = MvcApplication.GetInstance<IRoomService>();
            var lstRoomNew = roomservice.showMoreNewRoom(pageIndex + 1, 6);
            return Json(new { content = Helper.RenderViewToString(this.ControllerContext, "../Global/Rooms", lstRoomNew) });
        }

        public ActionResult Search(FormCollection col)
        {
            string txt = col["where"];
            var roomservice = MvcApplication.GetInstance<IRoomService>();
            var accountservice = MvcApplication.GetInstance<IAccountService>();
            var districtservice = MvcApplication.GetInstance<IDistrictService>();
            var areaservice = MvcApplication.GetInstance<IAreaService>();
            if (accountservice.searchAccountByName(txt) != null)
            {
                Account acc = accountservice.searchAccountByName(txt);
                return RedirectToAction("Index", "Host", new { id = acc.Id });
            }
            else if (roomservice.searchRoomByName(txt) != null)
            {
                return RedirectToAction("Index", "Room", new { id = roomservice.searchRoomByName(txt).Id });

            }
            else if (districtservice.searchDistrictByName(txt) != null)
            {
                return RedirectToAction("Index", "District", new { id = districtservice.searchDistrictByName(txt).Id });

            }
            else if (areaservice.searchAreaByName(txt) != null)
            {
                return RedirectToAction("Index", "Area", new { id = areaservice.searchAreaByName(txt).Id });

            }
            else

                return RedirectToAction("Index","Home");
        }

        public JsonResult AutoCompleteSearch(string term)
        {           
            var areaservice = MvcApplication.GetInstance<IAreaService>();
            var roomservice = MvcApplication.GetInstance<IRoomService>();            
            var districtservice = MvcApplication.GetInstance<IDistrictService>();
            var areas = areaservice.SuggestAreas(term).Select(c => c.Area_Name ).Distinct();
            var districts = districtservice.SuggestDistricts(term).Select(d => d.District_Name ).Distinct();
            var rooms = roomservice.SuggestRooms(term).Select(r => r.Room_Name).ToList();
          
            var x = new List<object>();
            x.AddRange(areas);
            x.AddRange(districts);
            x.AddRange(rooms);
            var z = x.Distinct();
            return Json(z,JsonRequestBehavior.AllowGet);
        }
    }
}
